package test2
//包括属性有音乐列表、当前播放的音乐、播放模式等，并能够实现添加音乐、删除音乐、播放音乐等方法
class MusicPlayer {
  var playlist: List[String] = Nil     // 音乐列表
  var currentSong: String = ""         // 当前播放的音乐
  var playMode: String = "atuo"        // 播放模式，默认为循环播放
  //播放音乐
  def addSong(song: String): Unit = {
    playlist = playlist :+ song
  }
  def removeSong(song: String): Unit = {
    playlist = playlist.filter(_ != song)
  }
//播放音乐
  def play(): Unit = {
    currentSong = playlist.headOption.getOrElse("")
  }
  // 根据当前播放模式选择下一首音乐
  def next(): Unit = {
    playMode match {
      case "loop" => loopPlayNext()
      case "shuffle" => shufflePlayNext()
      case "single" => singlePlayNext()
      case _ => println("Unknown play mode!")
    }
  }

  def loopPlayNext(): Unit = {
    val index = playlist.indexOf(currentSong)
    if (index == playlist.length - 1) {
      currentSong = playlist.headOption.getOrElse("")
    } else {
      currentSong = playlist(index + 1)
    }
  }

  def shufflePlayNext(): Unit = {
    currentSong = scala.util.Random.shuffle(playlist).headOption.getOrElse("")
  }

  def singlePlayNext(): Unit = {

    if (playlist.contains(currentSong)) {
      currentSong = currentSong
    } else {
      currentSong = playlist.headOption.getOrElse("")
    }
  }
}
